#include "..\..\Header\Interface\interface.h"

using namespace sf;
using namespace std;
MenuMultijoueur::MenuMultijoueur()
{
    Option m_option;
    if(!m_bufferclic.loadFromFile("sounds/clic.ogg"))
    {
        cout << "Erreur dans le chargement des clics" << endl;
    }
    else
    {
        m_soundclic.setBuffer(m_bufferclic);
        m_soundclic.setVolume(m_option.GetVolume()/1.5);
    }
    if(!image_debut.loadFromFile("Picture/menu/debut_multijoueur.png"))
    {
        cout << "Erreur dans le chargement du debut" << endl;
    }
    else
    {
        sprite_debut.setTexture(image_debut);
        sprite_debut.setPosition(200,100);
    }
    if(!image_fond.loadFromFile("Picture/menu/menu_multijoueur.png"))
    {
        cout << "Erreur dans le chargement du menu multijoueur" << endl;
    }
    else
    {
        sprite_fond.setTexture(image_fond);
    }
    if(!image_tank.loadFromFile("Picture/map/tank.png"))
    {
        cout << "Erreur dans le chargement des images des unités" << endl;
    }
    else
    {
        sprite_tank.setTexture(image_tank);
    }
    if(!image_bouton.loadFromFile("Picture/menu/bouton.png"))
    {
        cout << "Erreur dans le chargement des images des boutons" << endl;
    }
    else
    {
        sprite_down.setTexture(image_bouton);
        sprite_down.setTextureRect(IntRect(0,0,40,40));
        sprite_down.setPosition(15,485);

        sprite_up.setTexture(image_bouton);
        sprite_up.setTextureRect(IntRect(40,0,80,40));
        sprite_up.setPosition(145,485);

        sprite_left.setTexture(image_bouton);
        sprite_left.setTextureRect(IntRect(80,0,120,40));
        sprite_left.setPosition(740,70);

        sprite_right.setTexture(image_bouton);
        sprite_right.setTextureRect(IntRect(120,0,160,40));
        sprite_right.setPosition(420,70);

        sprite_unite.setTexture(image_bouton);
        sprite_unite.setTextureRect(IntRect(160,0,320,30));
    }
    if(!font.loadFromFile("font.ttf"))
    {
        cout << "Erreur aucune police d'écriture n'est charger." << endl;
    }
    else
    {
        texte.setFont(font);
        texte.setColor(Color(0,0,0));
        status.setFont(font);
        status.setColor(Color(255,255,255));
        status.setCharacterSize(20);
        status.setPosition(25,565);
    }
}
std::vector<Unite*> MenuMultijoueur::Boucle(sf::RenderWindow *app, int *ia)
{
    bool menuOpen = true, debutOpen = true;
    vector<Unite*> m_unite;
    int curseur_unite_disp = 0;
    ifstream unite("");
    ifstream tank("file/TFile.sw");
    ifstream infant("file/IFile.sw");
    ifstream heli("file/HFile.sw");
    ifstream canon("file/CFile.sw");
    ifstream missile("file/MFile.sw");
    ifstream arme("file/AFile.sw");
    string text;
    string oss, oss1;
    string caracteristique(""), text_tmp("");
    nombre_unite_disponible = -1;
    id_uniteselect = 0;
    equipe_actu = 0;
    equipe_max = 2;
    bool souris, souris_tmp;
    float time;
    sf::Clock clock;
    int curseur=0;
    int point = 100;
    if(tank)
    {
        while(getline(tank, text))
        {
            unite_disp.push_back(new std::vector<std::string > );
            stringstream stream(text);
            string mot("ah");
            stream >> mot;
            while(!stream.eof())
            {
                unite_disp[unite_disp.size()-1]->push_back(mot);
                cout << mot << endl;
                stream >> mot;
            }
            stream >> mot;
            cout << mot << endl;
            unite_disp[unite_disp.size()-1]->push_back(mot);
        }
    }
    else
    {
        cout << "Erreur dans le chargement du fichier du tank" << endl;
    }
    text = "";
    if(infant)
    {
        while(getline(infant, text))
        {
            unite_disp.push_back(new std::vector<std::string > );
            stringstream stream(text);
            string mot("ah");
            stream >> mot;
            while(!stream.eof())
            {
                unite_disp[unite_disp.size()-1]->push_back(mot);
                cout << mot << endl;
                stream >> mot;
            }
            stream >> mot;
            cout << mot << endl;
            unite_disp[unite_disp.size()-1]->push_back(mot);
        }
    }
    else
    {
        cout << "Erreur dans le chargement du fichier d'infanterie" << endl;
    }
    if(heli)
    {
        while(getline(heli, text))
        {
            unite_disp.push_back(new std::vector<std::string > );
            stringstream stream(text);
            string mot("ah");
            stream >> mot;
            while(!stream.eof())
            {
                unite_disp[unite_disp.size()-1]->push_back(mot);
                cout << mot << endl;
                stream >> mot;
            }
            stream >> mot;
            cout << mot << endl;
            unite_disp[unite_disp.size()-1]->push_back(mot);
        }
    }
    else
    {
        cout << "Erreur dans le chargement du fichier des canons" << endl;
    }
    if(arme)
    {
        while(getline(arme, text))
        {
            arme_disp.push_back(new std::vector<std::string > );
            stringstream stream(text);
            string mot("ah");
            stream >> mot;
            while(!stream.eof())
            {
                arme_disp[arme_disp.size()-1]->push_back(mot);
                cout << mot << endl;
                stream >> mot;
            }
            stream >> mot;
            cout << mot << endl;
            arme_disp[arme_disp.size()-1]->push_back(mot);
        }
    }
    else
    {
        cout << "Erreur dans le chargement du fichier des armes" << endl;
    }
    if(missile)
    {
        while(getline(missile, text))
        {
            missile_disp.push_back(new std::vector<std::string > );
            stringstream stream(text);
            string mot("ah");
            stream >> mot;
            while(!stream.eof())
            {
                missile_disp[missile_disp.size()-1]->push_back(mot);
                cout << mot << endl;
                stream >> mot;
            }
            stream >> mot;
            cout << mot << endl;
            missile_disp[missile_disp.size()-1]->push_back(mot);
        }
    }
    else
    {
        cout << "Erreur dans le chargement du fichier des missiles" << endl;
    }
    if(canon)
    {
        while(getline(canon, text))
        {
            canon_disp.push_back(new std::vector<std::string > );
            stringstream stream(text);
            string mot("ah");
            stream >> mot;
            while(!stream.eof())
            {
                canon_disp[canon_disp.size()-1]->push_back(mot);
                cout << mot << endl;
                stream >> mot;
            }
            stream >> mot;
            cout << mot << endl;
            canon_disp[canon_disp.size()-1]->push_back(mot);
        }
    }
    else
    {
        cout << "Erreur dans le chargement du fichier des canons" << endl;
    }
    nombre_unite_disponible = unite_disp.size()-1;
    sf::Clock temps;
    while(debutOpen && app->isOpen())
    {
        souris = false;
        if(sf::Mouse::isButtonPressed(Mouse::Left) && !souris_tmp)
        {
            souris = true;
            souris_tmp = true;
            temps.restart();
        }
        else if(!sf::Mouse::isButtonPressed(Mouse::Left))
        {
            souris_tmp = false;
        }
        if(temps.getElapsedTime().asSeconds()>1 && souris_tmp)
        {
            souris = true;
        }
        if(souris)
        {
            if(sf::Mouse::getPosition().x>450 && sf::Mouse::getPosition().x<480)
            {
                if(sf::Mouse::getPosition().y>165 && sf::Mouse::getPosition().y<195)
                {
                    if(equipe_max>2)
                        equipe_max--;
                    m_soundclic.play();
                }
                if(sf::Mouse::getPosition().y>255 && sf::Mouse::getPosition().y<285)
                {
                    if(*ia>0)
                        *ia--;
                    m_soundclic.play();
                }
                if(sf::Mouse::getPosition().y>345 && sf::Mouse::getPosition().y<375)
                {
                    if(point>0)
                        point--;
                    m_soundclic.play();
                }
            }
            else if (sf::Mouse::getPosition().x>525 && sf::Mouse::getPosition().x<555)
            {
                if(sf::Mouse::getPosition().y>165 && sf::Mouse::getPosition().y<195)
                {
                    if(equipe_max<4)
                        equipe_max++;
                    m_soundclic.play();
                }
                if(sf::Mouse::getPosition().y>255 && sf::Mouse::getPosition().y<285)
                {
                    if(*ia<equipe_max-1)
                        *ia++;
                    m_soundclic.play();
                }
                if(sf::Mouse::getPosition().y>345 && sf::Mouse::getPosition().y<375)
                {
                    if(point<250)
                        point++;
                    m_soundclic.play();
                }
            }
            else if (sf::Mouse::getPosition().x>350 && sf::Mouse::getPosition().x< 450 && sf::Mouse::getPosition().y>420 && sf::Mouse::getPosition().y<450)
            {
                debutOpen = false;
                    m_soundclic.play();
            }
        }
        sf::Event Event;
        while(app->pollEvent(Event))
        {
            if(Event.type == Event::Closed)
            {
                app->close();
                debutOpen = false;
            }
        }
        if(*ia == equipe_max)
        {
            *ia--;
        }
        app->clear();
        app->draw(sprite_debut);
        texte.setCharacterSize(20);
        ostringstream stream;
        stream << equipe_max;
        texte.setString(stream.str());
        texte.setPosition(490,165);
        texte.setColor(Color::White);
        app->draw(texte);
        stream.str("");
        stream << *ia;
        texte.setString(stream.str());
        texte.setPosition(490,255);
        texte.setColor(Color::White);
        app->draw(texte);
        stream.str("");
        stream << point;
        texte.setString(stream.str());
        texte.setPosition(490,345);
        texte.setColor(Color::White);
        app->draw(texte);
        stream.str("");
        app->display();
    }
    for(int i(0);i<4;i++)
        id_equipepoint[i] = point;
    souris_tmp = false;
    souris = false;
    if(app->isOpen());
    while(menuOpen && app->isOpen())
    {
        if(sf::Mouse::isButtonPressed(Mouse::Left) && !souris_tmp)
        {
            souris = true;
            souris_tmp = true;
        }
        else if(!sf::Mouse::isButtonPressed(Mouse::Left))
        {
            souris_tmp = false;
        }
        status.setString("");
        if(sf::Mouse::getPosition().x > 665 && sf::Mouse::getPosition().y > 550)
        {
            if(souris)
            {
                bool erreur = false;
                for(int i(0);i<equipe_max;i++)
                {
                    if(equipe_unite[i].size() == 0)
                    {
                        erreur = true;
                    }
                }
                if(!erreur)
                    menuOpen = false;

                m_soundclic.play();
            }
            else
            {
                status.setString("Commencer la partie !");
            }
        }
        if(sf::Mouse::getPosition().x > 15 && sf::Mouse::getPosition().x < 55 && sf::Mouse::getPosition().y > 485 && sf::Mouse::getPosition().y <525)
        {
            sprite_down.setTextureRect(IntRect(0,40,40,80));
            if(souris)
            {
                if(curseur_unite_disp+10<=nombre_unite_disponible)
                {
                    curseur_unite_disp++;
                    m_soundclic.play();
                }
            }
        }
        else
        {
            sprite_down.setTextureRect(IntRect(0,0,40,40));
        }
        if(sf::Mouse::getPosition().x > 145 && sf::Mouse::getPosition().x < 185 && sf::Mouse::getPosition().y > 485 && sf::Mouse::getPosition().y <525)
        {
            sprite_up.setTextureRect(IntRect(40,40,80,80));
            if(souris)
            {
                if(curseur_unite_disp > 0)
                {
                    curseur_unite_disp--;
                    m_soundclic.play();
                }

            }
        }
        else
        {
            sprite_up.setTextureRect(IntRect(40,0,80,40));
        }
        if(sf::Mouse::getPosition().x > 400 && sf::Mouse::getPosition().x < 800 && sf::Mouse::getPosition().y > 60 && sf::Mouse::getPosition().y <120)
        {
            status.setString("Equipe actuel");
        }
        if(sf::Mouse::getPosition().x > 740 && sf::Mouse::getPosition().x < 780 && sf::Mouse::getPosition().y > 70 && sf::Mouse::getPosition().y <110)
        {
            if(souris && equipe_actu < equipe_max-1)
            {
                equipe_actu++;
                m_soundclic.play();
            }
            status.setString("Equipe suivante");
        }
        if(sf::Mouse::getPosition().x > 420 && sf::Mouse::getPosition().x < 460 && sf::Mouse::getPosition().y > 70 && sf::Mouse::getPosition().y <110)
        {
            if(souris && equipe_actu > 0)
            {
                equipe_actu--;
                m_soundclic.play();
            }
            status.setString("Equipe precedente");
        }
        if(sf::Mouse::getPosition().x > 200 && sf::Mouse::getPosition().x < 400 && sf::Mouse::getPosition().y > 60 && sf::Mouse::getPosition().y <260)
        {
            status.setString("Texture de l'unite selectionner");
        }
        if(sf::Mouse::getPosition().x > 200 && sf::Mouse::getPosition().x < 400 && sf::Mouse::getPosition().y > 260 && sf::Mouse::getPosition().y <550)
        {
            status.setString("Caracteristique de l'unite selectionner");
        }
        Event Event;
        while(app->pollEvent(Event))
        {
            if(Event.type == Event::Closed)
            {
                app->close();
                menuOpen = false;
            }
        }
        app->clear();
        app->draw(sprite_fond);
        ostringstream oss_temps;
        int contact_id = 0;
        texte.setCharacterSize(16);
        int pas_id = -1;
        while(contact_id <= nombre_unite_disponible && contact_id <= 10)
        {
            if(sf::Mouse::getPosition().x > 15 && sf::Mouse::getPosition().x < 175 && sf::Mouse::getPosition().y > 80+(contact_id*35) && sf::Mouse::getPosition().y < 110+(contact_id*35))
            {
                if(souris && atoi((*unite_disp[contact_id+curseur_unite_disp])[1].c_str()) <= id_equipepoint[equipe_actu] && equipe_unite[equipe_actu].size()<10)
                {
                    id_equipepoint[equipe_actu]-=atoi((*unite_disp[contact_id+curseur_unite_disp])[1].c_str());
                    equipe_unite[equipe_actu].push_back(contact_id);
                    m_soundclic.play();
                }
                texte.setString("Niveau : " + (*unite_disp[contact_id+curseur_unite_disp])[1]);
                texte.setPosition(215,280);
                app->draw(texte);
                caracteristique = "Arme : ";
                if((*unite_disp[contact_id+curseur_unite_disp])[0] == "TANKLOURD" || (*unite_disp[contact_id+curseur_unite_disp])[0] == "TANKLEGER")
                {
                    int id = atoi((*unite_disp[contact_id+curseur_unite_disp])[2].c_str());
                    if(id < canon_disp.size())
                        caracteristique += (*canon_disp[id])[0].c_str();
                    else
                        caracteristique += "ERROR";
                }
                else if((*unite_disp[contact_id+curseur_unite_disp])[0] == "HELICOLOURD" || (*unite_disp[contact_id+curseur_unite_disp])[0] == "HELICOLEGER")
                {
                    int id = atoi((*unite_disp[contact_id+curseur_unite_disp])[2].c_str());
                    if(id < missile_disp.size())
                        caracteristique += (*missile_disp[id])[0].c_str();
                    else
                        caracteristique += "ERROR";
                }
                else
                {
                    int id = atoi((*unite_disp[contact_id+curseur_unite_disp])[2].c_str());
                    if(id < arme_disp.size())
                        caracteristique += (*arme_disp[id])[0].c_str();
                    else
                        caracteristique += "ERROR";
                }
                texte.setString(caracteristique);
                texte.setPosition(215,298);
                app->draw(texte);

                if((*unite_disp[contact_id+curseur_unite_disp])[0] == "TANKLOURD" || (*unite_disp[contact_id+curseur_unite_disp])[0] == "TANKLEGER")
                    caracteristique = "Puissance : ";
                else
                    caracteristique = "Precision : ";

                texte.setString(caracteristique+(*unite_disp[contact_id+curseur_unite_disp])[4]);
                texte.setPosition(215,316);
                app->draw(texte);

                caracteristique = "Vie : ";
                texte.setString(caracteristique+(*unite_disp[contact_id+curseur_unite_disp])[5]);
                texte.setPosition(215,334);
                app->draw(texte);

                caracteristique = "Deplacement : ";
                texte.setString(caracteristique+(*unite_disp[contact_id+curseur_unite_disp])[3]);
                texte.setPosition(215,352);
                app->draw(texte);
                curseur+=3;
                int x1,x2,x3,x4;
                if(!image_tank.loadFromFile((*unite_disp[contact_id+curseur_unite_disp])[8]))
                {
                    cout << "Chargement failed ... " << endl;
                }
                if((*unite_disp[contact_id+curseur_unite_disp])[0] == "TANKLEGER" || (*unite_disp[contact_id+curseur_unite_disp])[0] == "TANKLOURD")
                {
                    x1 = atoi((*unite_disp[contact_id+curseur_unite_disp])[9].c_str());
                    x2 = atoi((*unite_disp[contact_id+curseur_unite_disp])[10].c_str());
                    x3 = atoi((*unite_disp[contact_id+curseur_unite_disp])[11].c_str());
                    x4 = atoi((*unite_disp[contact_id+curseur_unite_disp])[12].c_str());
                }
                else if ((*unite_disp[contact_id+curseur_unite_disp])[0] == "HELICOLEGER" || (*unite_disp[contact_id+curseur_unite_disp])[1] == "HELICOLOURD")
                {
                    x1=0;
                    x2=0;
                    x3=image_tank.getSize().x / atoi((*unite_disp[contact_id+curseur_unite_disp])[9].c_str());
                    x4=image_tank.getSize().y;
                }
                else
                {
                    x1=0;
                    x2=0;
                    x3=image_tank.getSize().x / atoi((*unite_disp[contact_id+curseur_unite_disp])[9].c_str());
                    x4=image_tank.getSize().y;
                }

                sprite_tank.setTexture(image_tank);
                sprite_tank.setTextureRect(IntRect(x1,x2,x3,x4));
                sprite_tank.setPosition(300-sprite_tank.getLocalBounds().width/2,160-sprite_tank.getLocalBounds().height/2);
                sprite_tank.setScale(1,1);
                /*if(sprite_tank.GetSize().x > sprite_tank.GetSize().y)
                {
                    sprite_tank.SetScale(180/sprite_tank.GetSize().x,180/sprite_tank.GetSize().x);
                }
                else
                {
                    sprite_tank.SetScale(180/sprite_tank.GetSize().y,180/sprite_tank.GetSize().y);
                }*/
                app->draw(sprite_tank);
            }
            contact_id++;
        }

        texte.setString("CHOIX DE L'EQUIPE");
        texte.setCharacterSize(50);
        texte.setPosition(400-texte.getLocalBounds().width/2,0);
        texte.setColor(Color(0,0,0));
        app->draw(texte);
        app->draw(status);
        switch(equipe_actu)
        {
            case 0:
                texte.setString("ROUGE");
                texte.setColor(Color::Red);
                break;
            case 1:
                texte.setString("BLEU");
                texte.setColor(Color::Blue);
                break;
            case 2:
                texte.setString("VERT");
                texte.setColor(Color::Green);
                break;
            case 3:
                texte.setString("JAUNE");
                texte.setColor(Color::Yellow);
                break;
        }
        texte.setCharacterSize(40);
        texte.setPosition(600-texte.getLocalBounds().width/2,80-texte.getLocalBounds().height/2);
        app->draw(texte);
        texte.setColor(Color::White);
        ostringstream point;
        if(id_equipepoint[equipe_actu] <2)
            point << "Point disponible : " << id_equipepoint[equipe_actu];
        else
            point << "Points disponibles : " << id_equipepoint[equipe_actu];
        texte.setCharacterSize(35);
        texte.setString(point.str());
        texte.setPosition(415,485);
        app->draw(texte);
        texte.setCharacterSize(12);
        for(int i(0);i<=nombre_unite_disponible;i++)
        {
            if(i>=curseur_unite_disp && i <= curseur_unite_disp+10)
            {
                sprite_unite.setPosition(15,80+(i-curseur_unite_disp)*35);
                texte.setPosition(23,87+(i-curseur_unite_disp)*35);
                texte.setString((*unite_disp[i])[0]);
                if(sf::Mouse::getPosition().x > 15 && sf::Mouse::getPosition().x < 175 && sf::Mouse::getPosition().y > 80+(i-curseur_unite_disp)*35 && sf::Mouse::getPosition().y < 110+(i-curseur_unite_disp)*35)
                {
                    sprite_unite.setTextureRect(IntRect(160,40,320,70));
                }
                else
                {
                    sprite_unite.setTextureRect(IntRect(160,0,320,30));
                }
                app->draw(sprite_unite);
                app->draw(texte);

            }
        }
        for(int i(0);i<equipe_unite[equipe_actu].size();i++)
        {
            if(i<5)
            {
                if(sf::Mouse::getPosition().x>415 && sf::Mouse::getPosition().x<575 && sf::Mouse::getPosition().y>145+i*55 && sf::Mouse::getPosition().y<175+i*55)
                {
                    if(souris)
                    {
                        m_soundclic.play();
                        id_equipepoint[equipe_actu] += atoi((*unite_disp[equipe_unite[equipe_actu][i]])[1].c_str());
                        for(int j(i); j<equipe_unite[equipe_actu].size()-1;j++)
                        {
                            equipe_unite[equipe_actu][j] = equipe_unite[equipe_actu][j+1];
                        }
                        equipe_unite[equipe_actu].pop_back();
                    }
                    else
                    {
                        texte.setCharacterSize(16);
                        texte.setString("Niveau : " + (*unite_disp[equipe_unite[equipe_actu][i]])[1]);
                        texte.setPosition(215,280);
                        app->draw(texte);

                        caracteristique = "Canon : ";
                        texte.setString(caracteristique + (*unite_disp[equipe_unite[equipe_actu][i]])[2]);
                        texte.setPosition(215,298);
                        app->draw(texte);

                        if((*unite_disp[equipe_unite[equipe_actu][i]])[0] == "TANKLOURD" || (*unite_disp[equipe_unite[equipe_actu][i]])[0] == "TANKLEGER")
                            caracteristique = "Puissance : ";
                        else
                            caracteristique = "Precision : ";

                        texte.setString(caracteristique+(*unite_disp[equipe_unite[equipe_actu][i]])[4]);
                        texte.setPosition(215,316);
                        app->draw(texte);

                        caracteristique = "Vie : ";
                        texte.setString(caracteristique+(*unite_disp[equipe_unite[equipe_actu][i]])[5]);
                        texte.setPosition(215,334);
                        app->draw(texte);

                        caracteristique = "Deplacement : ";
                        texte.setString(caracteristique+(*unite_disp[equipe_unite[equipe_actu][i]])[3]);
                        texte.setPosition(215,352);
                        app->draw(texte);
                        curseur+=3;
                        int x1,x2,x3,x4;
                        if(!image_tank.loadFromFile((*unite_disp[equipe_unite[equipe_actu][i]])[8]))
                        {
                            cout << "Chargement failed ... " << endl;
                        }
                        if((*unite_disp[equipe_unite[equipe_actu][i]])[0] == "TANKLEGER" || (*unite_disp[equipe_unite[equipe_actu][i]])[0] == "TANKLOURD")
                        {
                            x1 = atoi((*unite_disp[equipe_unite[equipe_actu][i]])[9].c_str());
                            x2 = atoi((*unite_disp[equipe_unite[equipe_actu][i]])[10].c_str());
                            x3 = atoi((*unite_disp[equipe_unite[equipe_actu][i]])[11].c_str());
                            x4 = atoi((*unite_disp[equipe_unite[equipe_actu][i]])[12].c_str());
                        }
                        else if ((*unite_disp[equipe_unite[equipe_actu][i]])[0] == "HELICOLEGER" || (*unite_disp[equipe_unite[equipe_actu][i]])[1] == "HELICOLOURD")
                        {
                            x1=0;
                            x2=0;
                            x3=image_tank.getSize().x / atoi((*unite_disp[equipe_unite[equipe_actu][i]])[9].c_str());
                            x4=image_tank.getSize().y;
                        }
                        else
                        {
                            x1=0;
                            x2=0;
                            x3=image_tank.getSize().x / atoi((*unite_disp[equipe_unite[equipe_actu][i]])[9].c_str());
                            x4=image_tank.getSize().y;
                        }

                        sprite_tank.setTexture(image_tank);
                        sprite_tank.setTextureRect(IntRect(x1,x2,x3,x4));
                        sprite_tank.setPosition(300-sprite_tank.getLocalBounds().width/2,160-sprite_tank.getLocalBounds().height/2);
                        sprite_tank.setScale(1,1);
                        app->draw(sprite_tank);
                    }
                    sprite_unite.setTextureRect(IntRect(160,40,320,70));
                }
                else
                {
                    sprite_unite.setTextureRect(IntRect(160,0,320,30));
                }
                texte.setCharacterSize(12);
                sprite_unite.setPosition(415,145+i*55);
                texte.setPosition(423,152+i*55);
                texte.setString((*unite_disp[equipe_unite[equipe_actu][i]])[0]);
                app->draw(sprite_unite);
                app->draw(texte);
            }
            else
            {
                if(sf::Mouse::getPosition().x>620 && sf::Mouse::getPosition().x<780 && sf::Mouse::getPosition().y>145+(i-5)*55 && sf::Mouse::getPosition().y<175+(i-5)*55)
                {
                    if(souris)
                    {
                        id_equipepoint[equipe_actu] += atoi((*unite_disp[equipe_unite[equipe_actu][i]])[1].c_str());
                        for(int j(i); j<equipe_unite[equipe_actu].size()-1;j++)
                        {
                            equipe_unite[equipe_actu][j] = equipe_unite[equipe_actu][j+1];
                        }
                        equipe_unite[equipe_actu].pop_back();
                    }
                    else
                    {
                        texte.setCharacterSize(16);
                        texte.setString("Niveau : " + (*unite_disp[equipe_unite[equipe_actu][i]])[1]);
                        texte.setPosition(215,280);
                        app->draw(texte);

                        caracteristique = "Canon : ";
                        texte.setString(caracteristique + (*unite_disp[equipe_unite[equipe_actu][i]])[2]);
                        texte.setPosition(215,298);
                        app->draw(texte);

                        if((*unite_disp[equipe_unite[equipe_actu][i]])[0] == "TANKLOURD" || (*unite_disp[equipe_unite[equipe_actu][i]])[0] == "TANKLEGER")
                            caracteristique = "Puissance : ";
                        else
                            caracteristique = "Precision : ";

                        texte.setString(caracteristique+(*unite_disp[equipe_unite[equipe_actu][i]])[4]);
                        texte.setPosition(215,316);
                        app->draw(texte);

                        caracteristique = "Vie : ";
                        texte.setString(caracteristique+(*unite_disp[equipe_unite[equipe_actu][i]])[5]);
                        texte.setPosition(215,334);
                        app->draw(texte);

                        caracteristique = "Deplacement : ";
                        texte.setString(caracteristique+(*unite_disp[equipe_unite[equipe_actu][i]])[3]);
                        texte.setPosition(215,352);
                        app->draw(texte);
                        curseur+=3;
                        int x1,x2,x3,x4;
                        if(!image_tank.loadFromFile((*unite_disp[equipe_unite[equipe_actu][i]])[8]))
                        {
                            cout << "Chargement failed ... " << endl;
                        }
                        if((*unite_disp[equipe_unite[equipe_actu][i]])[0] == "TANKLEGER" || (*unite_disp[equipe_unite[equipe_actu][i]])[0] == "TANKLOURD")
                        {
                            x1 = atoi((*unite_disp[equipe_unite[equipe_actu][i]])[9].c_str());
                            x2 = atoi((*unite_disp[equipe_unite[equipe_actu][i]])[10].c_str());
                            x3 = atoi((*unite_disp[equipe_unite[equipe_actu][i]])[11].c_str());
                            x4 = atoi((*unite_disp[equipe_unite[equipe_actu][i]])[12].c_str());
                        }
                        else if ((*unite_disp[equipe_unite[equipe_actu][i]])[0] == "HELICOLEGER" || (*unite_disp[equipe_unite[equipe_actu][i]])[1] == "HELICOLOURD")
                        {
                            x1=0;
                            x2=0;
                            x3=image_tank.getSize().x / atoi((*unite_disp[equipe_unite[equipe_actu][i]])[9].c_str());
                            x4=image_tank.getSize().y;
                        }
                        else
                        {
                            x1=0;
                            x2=0;
                            x3=image_tank.getSize().x / atoi((*unite_disp[equipe_unite[equipe_actu][i]])[9].c_str());
                            x4=image_tank.getSize().y;
                        }

                        sprite_tank.setTexture(image_tank);
                        sprite_tank.setTextureRect(IntRect(x1,x2,x3,x4));
                        sprite_tank.setPosition(300-sprite_tank.getLocalBounds().width/2,160-sprite_tank.getLocalBounds().height/2);
                        sprite_tank.setScale(1,1);
                        app->draw(sprite_tank);
                    }
                    sprite_unite.setTextureRect(IntRect(160,40,320,70));
                }
                else
                {
                    sprite_unite.setTextureRect(IntRect(160,0,320,30));
                }
                texte.setCharacterSize(12);
                sprite_unite.setPosition(620,145+(i-5)*55);
                texte.setPosition(628,152+(i-5)*55);
                texte.setString((*unite_disp[equipe_unite[equipe_actu][i]])[0]);
                app->draw(sprite_unite);
                app->draw(texte);
            }
        }


        app->draw(sprite_down);
        app->draw(sprite_left);
        app->draw(sprite_right);
        app->draw(sprite_up);
        app->display();
        souris = false;
    }
    string Type("");
    string FichierUnite("");
    string FichierCanon("");
    string Recherche("");
    string TextureArme("");
    curseur = 0;
    int pas_id = 0;
    float puissance, vie, deplacement, explosion, degat, x1,x2,x3,x4, afc1, afc2, c1,c2, a1,a2,a3,a4, ac1, ac2;
    float nbtir, repeat, precision;
    bool spe1, spe2;
    // A programmé, convertir les unités...
    for(int i(0);i<equipe_max;i++)
    {
        for(int j(0);j<equipe_unite[i].size();j++)
        {
            std::vector<std::string> unite_temp = (*unite_disp[equipe_unite[i][j]]);
            bool physique;
            if(unite_temp[0] == "TANKLEGER" || unite_temp[0] == "TANKLOURD")
            {
                physique = true;
                IntRect subV(atoi(unite_temp[9].c_str()),atoi(unite_temp[10].c_str()),atoi(unite_temp[11].c_str()),atoi(unite_temp[12].c_str()));
                IntRect subC(atoi(unite_temp[13].c_str()),atoi(unite_temp[14].c_str()),atoi(unite_temp[15].c_str()),atoi(unite_temp[16].c_str()));
                m_unite.push_back(new Tank(sf::Vector2f(100,100),unite_temp[8],subV, subC,
                                            Vector2f(atoi(unite_temp[17].c_str()),atoi(unite_temp[18].c_str())),
                                           Vector2f(atoi(unite_temp[19].c_str()),atoi(unite_temp[20].c_str())),
                                           Vector2f(atoi(unite_temp[21].c_str()),atoi(unite_temp[22].c_str())),
                                           Roquette(atoi( (*canon_disp[atoi(unite_temp[2].c_str())])[2].c_str() ),
                                                    (*canon_disp[atoi(unite_temp[2].c_str())])[1].c_str(),
                                                    atoi( (*canon_disp[atoi(unite_temp[2].c_str())])[3].c_str() )),i+1));
            }
            else if(unite_temp[0] == "HELICOLEGER" || unite_temp[0] == "HELICOLOURD")
            {
                physique = false;
                m_unite.push_back(new Helico(sf::Vector2f(100,100),unite_temp[8],atoi(unite_temp[9].c_str()),sf::Vector2f(atoi(unite_temp[10].c_str()),atoi(unite_temp[11].c_str())),
                                             Missile(atoi( (*missile_disp[atoi(unite_temp[2].c_str())])[1].c_str() ),
                                                    (*missile_disp[atoi(unite_temp[2].c_str())])[2].c_str(),
                                                    atoi( (*missile_disp[atoi(unite_temp[2].c_str())])[3].c_str() )),i+1));
            }
            else
            {
                physique = true;
                int sous_type;
                if(unite_temp[0] == "SOLDAT")
                {
                    sous_type = SOLDAT;
                }
                else if(unite_temp[0] == "MEDIC")
                {
                    sous_type = MEDIC;
                }
                else if(unite_temp[0] == "TECH")
                {
                    sous_type = TECH;
                }
                else
                {
                    sous_type = SNIPER;
                }
                if(sous_type == TECH)
                {
                    m_unite.push_back(new Infanterie(sf::Vector2f(100,100),unite_temp[8],atoi(unite_temp[9].c_str()),
                                                     Missile(atoi( (*missile_disp[atoi(unite_temp[2].c_str())])[1].c_str() ),
                                                    (*missile_disp[atoi(unite_temp[2].c_str())])[2].c_str(),
                                                    atoi( (*missile_disp[atoi(unite_temp[2].c_str())])[3].c_str() )),
                                                     sous_type,i+1));
                }
                else
                {
                    m_unite.push_back(new Infanterie(sf::Vector2f(100,100),unite_temp[8],atoi(unite_temp[9].c_str()),
                                                     Arme(atoi( (*arme_disp[atoi(unite_temp[2].c_str())])[1].c_str() ),
                                                          atoi( (*arme_disp[atoi(unite_temp[2].c_str())])[2].c_str() ),
                                                          atoi( (*arme_disp[atoi(unite_temp[2].c_str())])[3].c_str() ),
                                                          atoi( (*arme_disp[atoi(unite_temp[2].c_str())])[4].c_str() )),
                                                     sous_type,i+1));
                }
            }
            bool spe1(false), spe2(false);
            if(unite_temp[6] == "o")
            {
                spe1 == true;
            }
            if(unite_temp[7] == "o")
            {
                spe2 == true;
            }
            m_unite[m_unite.size()-1]->SetSpe(spe1, spe2);
            m_unite[m_unite.size()-1]->SetCaracter(atoi(unite_temp[3].c_str()),atoi(unite_temp[4].c_str()),atoi(unite_temp[5].c_str()),physique);
        }
    }
    //Equipe
    return m_unite;
}
MenuMultijoueur::~MenuMultijoueur()
{
    for(int i(0);i<unite_disp.size();i++)
        delete unite_disp[i];
    for(int i(0);i<arme_disp.size();i++)
        delete arme_disp[i];
    for(int i(0);i<canon_disp.size();i++)
        delete unite_disp[i];
    for(int i(0);i<missile_disp.size();i++)
        delete unite_disp[i];
}
int MenuPrincipal(sf::RenderWindow *app, bool animation)
{

    sf::Texture image_fond,image_menu,image_bouton;
    sf::Sprite sprite_fond,sprite_menu,sprite_solo,sprite_multi,sprite_option,sprite_quitter;
    sf::Text string_total;
    sf::Font font;
    int curseur(0), alpha(0);
    bool enCreation(animation);
    if(!image_fond.loadFromFile("picture/menu/menu_fond.png"))
    {
        cout << "Impossible de charger le fond.." << endl;
    }
    else
    {
        sprite_fond.setTexture(image_fond);
        sprite_fond.setPosition(0,0);
    }
    if(!image_menu.loadFromFile("picture/menu/menu_principal.png"))
    {
        cout << "Impossible de charger le menu..." << endl;
    }
    else
    {
        sprite_menu.setTexture(image_menu);
        if(enCreation)
            sprite_menu.setPosition(0,600);
        else
            sprite_menu.setPosition(0,0);
    }
    if(!image_bouton.loadFromFile("picture/menu/bouton_principal.png"))
    {
        cout << "Impossible de charger les bouton..." << endl;
    }
    else
    {
        sprite_solo.setTexture(image_bouton);
        sprite_solo.setTextureRect(IntRect(0,0,185,44));
        sprite_solo.setPosition(58,88);


        sprite_multi.setTexture(image_bouton);
        sprite_multi.setTextureRect(IntRect(0,0,185,44));
        sprite_multi.setPosition(58,148);


        sprite_option.setTexture(image_bouton);
        sprite_option.setTextureRect(IntRect(0,0,185,44));
        sprite_option.setPosition(58,208);


        sprite_quitter.setTexture(image_bouton);
        sprite_quitter.setTextureRect(IntRect(0,0,185,44));
        sprite_quitter.setPosition(58,268);
    }
    if(!font.loadFromFile("font.ttf"))
    {
        cout << "police non chargé ..." << endl;
    }
    else
    {
        string_total.setFont(font);
        string_total.setCharacterSize(25);
        if(enCreation)
            string_total.setColor(Color(255,255,255,0));
        else
            string_total.setColor(Color(255,255,255,255));
    }
    bool appouverte = true;
    int choix(0);
    while(appouverte && app->isOpen())
    {
        Event Event;
        while(app->pollEvent(Event))
        {
            if(Event.type == Event::Closed)
            {
                app->close();
            }
        }
        sf::Vector2f vecteur_souris(sf::Mouse::getPosition().x,sf::Mouse::getPosition().y);
        if(!enCreation)
        {
            if(CollisionBoiteVector(sprite_solo,vecteur_souris))
            {
                sprite_solo.setTextureRect(IntRect(0,44,185,88));
                if(sf::Mouse::isButtonPressed(Mouse::Left))
                {
                    appouverte = false;
                    choix = 1;
                }
            }
            else
            {
                sprite_solo.setTextureRect(IntRect(0,0,185,44));
            }
            if(CollisionBoiteVector(sprite_multi,vecteur_souris))
            {
                sprite_multi.setTextureRect(IntRect(0,44,185,88));
                if(sf::Mouse::isButtonPressed(Mouse::Left))
                {
                    appouverte = false;
                    choix = 2;
                }
            }
            else
            {
                sprite_multi.setTextureRect(IntRect(0,0,185,44));
            }
            if(CollisionBoiteVector(sprite_option,vecteur_souris))
            {
                sprite_option.setTextureRect(IntRect(0,44,185,88));
                if(sf::Mouse::isButtonPressed(Mouse::Left))
                {
                    appouverte = false;
                    choix = 3;
                }
            }
            else
            {
                sprite_option.setTextureRect(IntRect(0,0,185,44));
            }
            if(CollisionBoiteVector(sprite_quitter,vecteur_souris))
            {
                sprite_quitter.setTextureRect(IntRect(0,44,185,88));
                if(sf::Mouse::isButtonPressed(Mouse::Left))
                {
                    appouverte = false;
                    choix = 0;
                }
            }
            else
            {
                sprite_quitter.setTextureRect(IntRect(0,0,185,44));
            }
        }
        else
        {
            if(curseur<600)
                curseur+=5;
            if(curseur==600)
            {
                alpha+=5;
                for(int i(0);i<image_bouton.getSize().x;i++)
                    for(int j(0);j<image_bouton.getSize().y;j++)
                    {
                        sf::Color color_tmp = image_bouton.copyToImage().getPixel(i,j);
                        color_tmp.a = alpha;
                        image_bouton.copyToImage().setPixel(i,j,color_tmp);
                        string_total.setColor(Color(255,255,255,alpha));
                    }
                if(alpha>=255)
                    enCreation = false;
            }
            sprite_menu.setPosition(0,600-curseur);
        }
        app->clear();
        app->draw(sprite_fond);
        app->draw(sprite_menu);
        app->draw(sprite_solo);
        string_total.setPosition(sprite_solo.getPosition()+Vector2f(15,5));
        string_total.setString("PARTIE SOLO");
        app->draw(string_total);
        app->draw(sprite_multi);
        string_total.setPosition(sprite_multi.getPosition()+Vector2f(15,5));
        string_total.setString("MULTIJOUEUR");
        app->draw(string_total);
        app->draw(sprite_option);
        string_total.setPosition(sprite_option.getPosition()+Vector2f(15,5));
        string_total.setString("OPTION");
        app->draw(string_total);
        app->draw(sprite_quitter);
        string_total.setPosition(sprite_quitter.getPosition()+Vector2f(15,5));
        string_total.setString("QUITTER");
        app->draw(string_total);
        app->display();
    }
    return choix;
}
generationTerrain::generationTerrain()
{
    if(!m_bouton.loadFromFile("picture/menu/bouton_generateur.png"))
        cout << "impossible de charger les boutons generateurs..." << endl;
    else
    {
        m_genere.setTexture(m_bouton);
        m_genere.setTextureRect(IntRect(0,0,120,30));
        m_genere.setPosition(300,510);
        m_continuer.setTexture(m_bouton);
        m_continuer.setTextureRect(IntRect(0,30,120,60));
        m_continuer.setPosition(450,510);
        m_facile.setTexture(m_bouton);
        m_facile.setTextureRect(IntRect(0,90,60,105));
        m_facile.setPosition(200,175);
        m_moyen.setTexture(m_bouton);
        m_moyen.setTextureRect(IntRect(60,90,120,105));
        m_moyen.setPosition(200,193);
        m_difficile.setTexture(m_bouton);
        m_difficile.setTextureRect(IntRect(0,105,60,120));
        m_difficile.setPosition(200,211);
        m_relief.setTexture(m_bouton);
        m_relief.setTextureRect(IntRect(0,120,10,130));
        m_relief.setOrigin(5,5);
        m_relief.setPosition(245,335);
        m_pas.setTexture(m_bouton);
        m_pas.setTextureRect(IntRect(0,120,10,130));
        m_pas.setOrigin(5,5);
        m_pas.setPosition(245,400);
        m_sprite_curseur.setTexture(m_bouton);
        m_sprite_curseur.setTextureRect(IntRect(0,130,35,215));
        m_sprite_curseur.setOrigin(12,0);
        m_sprite_curseur.setPosition(250+45,75+55);
        m_sprite_collision_rect.setTexture(m_bouton);
        m_sprite_collision_rect.setTextureRect(IntRect(50,150,75,175));
        m_sprite_collision_rect.setOrigin(12,0);
        m_sprite_collision_rect.setPosition(300,210);
    }
    if(!m_interface.loadFromFile("picture/menu/menu_generation_terrain.png"))
        cout << "Impossible de charger l'interface..." << endl;
    else
        m_fond.setTexture(m_interface);
    if(!m_image_fond.loadFromFile("terrain/temporaire/fond_terrain.png"))
        cout << "Pas de fond..." << endl;
    else
    {
        m_sprite_fond.setTexture(m_image_fond);
        m_sprite_fond.setPosition(330,130);
    }
    if(!font.loadFromFile("font.ttf"))
    {
        cout << "Erreur aucune police d'écriture n'est charger." << endl;
    }
    else
    {
        m_seed.setFont(font);
        m_seed.setPosition(197,253);
        m_seed.setColor(Color::White);
        m_seed.setCharacterSize(20);
    }
    if(!m_image_select.loadFromFile("picture/menu/select_position_unite.png"))
        cout << "Pas d'image de selection..." << endl;
    else
    {
        m_sprite_select.setTexture(m_image_select);
        m_sprite_select.setPosition(250,100);
    }
    m_sprite_bouton.setTexture(m_bouton);
    m_sprite_bouton.setTextureRect(IntRect(0,215,120,245));
}
void generationTerrain::boucle(sf::RenderWindow *app)
{
    bool IsOpen(true), IsSeed(false), direction(false), select_position(false);
    int taille(6400), relief(60), pas(600), taillemax(6400-800), curseur(0);
    string seed("");
    float zoom(5);
    Clock clock;
    sf::Text tmp_string("");
    tmp_string.setFont(font);
    tmp_string.setColor(Color::White);
    m_seed.setString("");
    int posx[4] = {0}, selectionner(0), curseur_position(0);
    sf::Vector2f presouris(0,0);
    bool cliqueDroit(false), direction_clique(false);

    //Generation aléatoire du début
    Terrain::generationAlea(0,taille,pas,relief);
    m_image_collision.loadFromFile("terrain/temporaire/terrain.png");
    m_sprite_collision.setTexture(m_image_collision);
    taille = m_image_collision.getSize().x;
    if(taille == 0)
        taille = 6400;
    for(int i(0); i<taille/800;i++)
    {
        m_image_terre.push_back(new sf::Texture());
        string tmp_string("terrain/temporaire/terre");
        if(i==0)
            tmp_string += ".png";
        else
            tmp_string += to_string(i)+".png";
        if(!m_image_terre[i]->loadFromFile(tmp_string.c_str()))
        {
            cout << "ERREUR de chargement" << endl;
        }
            m_sprite_terre.push_back(sf::Sprite());
            m_sprite_terre[i].setTexture(*m_image_terre[i]);
            m_sprite_terre[i].setScale(0.4+(zoom/50),0.4+(zoom/50));
    }
    m_sprite_collision.setTextureRect(IntRect(0,0,2460,600));
    m_sprite_collision.setScale(0.083,0.083);
    m_sprite_collision.setPosition(295,150);
    curseur = 0;
    direction = false;
    posx[0] = 300;
    posx[1] = taille/5*2;
    posx[2] = taille/5*3;
    posx[3] = taille/5*4;
    while(IsOpen && app->isOpen())
    {
        if(!select_position)
        {
            if(direction)
                curseur -= 6-zoom;
            else
                curseur += 6-zoom;
            if(curseur > taillemax)
            {
                direction = true;
                curseur = 6400-800;
            }
            else if(curseur<0)
            {
                direction = false;
                curseur = 0;
            }
            cliqueDroit = false;
            if(sf::Mouse::isButtonPressed(Mouse::Left) && !direction_clique)
            {
                cliqueDroit = true;
                direction_clique = true;
            }
            if(!sf::Mouse::isButtonPressed(Mouse::Left))
            {
                direction_clique = false;
            }
        }
        Event Event;
        while(app->pollEvent(Event))
        {
            if(IsSeed && Event.type == Event::KeyPressed)
            {
                if(Event.key.code != sf::Keyboard::Back && seed.size()<9)
                {
                    switch(Event.key.code)
                    {
                        case Keyboard::Numpad0:
                        seed += "0";
                        break;

                        case Keyboard::Numpad1:
                        seed += "1";
                        break;

                        case Keyboard::Numpad2:
                        seed += "2";
                        break;

                        case Keyboard::Numpad3:
                        seed += "3";
                        break;

                        case Keyboard::Numpad4:
                        seed += "4";
                        break;

                        case Keyboard::Numpad5:
                        seed += "5";
                        break;

                        case Keyboard::Numpad6:
                        seed += "6";
                        break;

                        case Keyboard::Numpad7:
                        seed += "7";
                        break;

                        case Keyboard::Numpad8:
                        seed += "8";
                        break;

                        case Keyboard::Numpad9:
                        seed += "9";
                        break;

                        case Keyboard::Space:
                        seed += " ";
                        break;

                        default:
                        seed += Event.key.code;
                        break;
                    }
                }
                else if(seed != "" && Event.key.code == Keyboard::Back)
                {
                    string tmp_str("");
                    for(int i(0); i<seed.size()-1;i++)
                        tmp_str += seed[i];
                    seed = tmp_str;
                }
                m_seed.setString(seed);
            }
            if(Event.type == Event::Closed)
            {
                app->close();
                IsOpen = false;
            }
        }
        sf::Vector2f souris(sf::Mouse::getPosition().x,sf::Mouse::getPosition().y);
        if(!select_position)
        {
            if(CollisionBoiteVector(m_facile, souris) && sf::Mouse::isButtonPressed(Mouse::Left))
            {
                taille = 6400;
            }
            if(CollisionBoiteVector(m_moyen, souris) && sf::Mouse::isButtonPressed(Mouse::Left))
            {
                taille = 12800;
            }
            if(CollisionBoiteVector(m_difficile, souris) && sf::Mouse::isButtonPressed(Mouse::Left))
            {
                taille = 25600;
            }
            if(CollisionBoiteVector(m_continuer, souris) && sf::Mouse::isButtonPressed(Mouse::Left))
            {
                IsOpen = false;
            }
            if(souris.y < m_relief.getPosition().y + 10 && souris.y > m_relief.getPosition().y - 10  && sf::Mouse::isButtonPressed(Mouse::Left))
            {
                if(souris.x > 195 && souris.x < 295)
                {
                    m_relief.setPosition(souris.x, m_relief.getPosition().y);
                    relief = 50+souris.x-195;
                }
            }
            if(souris.y < m_pas.getPosition().y + 10 && souris.y > m_pas.getPosition().y - 10 && sf::Mouse::isButtonPressed(Mouse::Left))
            {
                if(souris.x > 195 && souris.x < 295)
                {
                    m_pas.setPosition(souris.x, m_pas.getPosition().y);
                    pas = 200+(souris.x-195)*8;
                }
            }
            if(CollisionBoiteVector(m_genere, souris) && sf::Mouse::isButtonPressed(Mouse::Left))
            {
                for(int i(0);i<m_image_terre.size();i++)
                {
                    m_image_terre.pop_back();
                    m_sprite_terre.pop_back();
                }
                taillemax = taille-800;
                cout << "TAILLE = " << taille << endl;
                int graine(0);
                for(int i(0);i<seed.size();i++)
                {
                    if(seed[i] < 10)
                        graine *= 10;
                    graine += seed[i];
                    if(i != seed.size()-1)
                        graine *= 100;
                }
                cout << graine << endl;
                Terrain::generationAlea(graine,taille,pas,relief);
                for(int i(0); i<taille/800;i++)
                {
                    m_image_terre.push_back(new sf::Texture());
                    string tmp_string("terrain/temporaire/terre");
                    if(i==0)
                        tmp_string += ".png";
                    else
                        tmp_string += to_string(i)+".png";
                    if(!m_image_terre[i]->loadFromFile(tmp_string.c_str()))
                    {
                        cout << "ERREUR de chargement" << endl;
                    }
                        m_sprite_terre.push_back(sf::Sprite());
                        m_sprite_terre[i].setTexture(*m_image_terre[i]);
                        m_sprite_terre[i].setScale(0.4+(zoom/50),0.4+(zoom/50));
                }
                m_image_collision.loadFromFile("terrain/temporaire/terrain.png");
                m_sprite_collision.setTexture(m_image_collision);
                taille = m_image_collision.getSize().x;
                m_sprite_collision.setTextureRect(IntRect(0,0,2460,600));
                m_sprite_collision.setScale(0.083,0.083);
                m_sprite_collision.setPosition(295,150);
                posx[0] = 300;
                posx[1] = taille/5*2;
                posx[2] = taille/5*3;
                posx[3] = taille/5*4;
                curseur = 0;
                direction = false;
            }
            if(souris.x > 190 && souris.x < 295 && souris.y > 250 && souris.y < 280 && sf::Mouse::isButtonPressed(Mouse::Left))
            {
                seed = "";
                m_seed.setString("");
                IsSeed =true;
            }
            else if (sf::Mouse::isButtonPressed(Mouse::Left))
            {
                IsSeed = false;
            }
        }
        app->clear();
        app->draw(m_sprite_fond);
        for(int i(0);i<m_sprite_terre.size();i++)
        {
            m_sprite_terre[i].setPosition(i*(800*(0.4+(zoom/50)))-(curseur*(0.4+(zoom/50)))+330,130+(600-(1200*(0.4+(zoom/50)))));
            m_sprite_terre[i].setScale(0.4+(zoom/50),0.4+(zoom/50));
            app->draw(m_sprite_terre[i]);
        }
        app->draw(m_fond);
        app->draw(m_genere);
        app->draw(m_continuer);
        app->draw(m_facile);
        app->draw(m_moyen);
        app->draw(m_difficile);
        app->draw(m_relief);
        app->draw(m_pas);
        app->draw(m_seed);
        m_sprite_bouton.setTextureRect(IntRect(0,215,120,245));
        m_sprite_bouton.setPosition(150,510);
        if(CollisionBoiteVector(m_sprite_bouton,souris) && !select_position)
        {
            m_sprite_bouton.setTextureRect(IntRect(0,245,120,275));
            if(cliqueDroit)
            {
                if(zoom > 1)
                    zoom--;
            }
        }
        tmp_string.setString("Zoom --");
        tmp_string.setPosition(155,505);
        app->draw(m_sprite_bouton);
        app->draw(tmp_string);
        m_sprite_bouton.setTextureRect(IntRect(0,215,120,245));
        m_sprite_bouton.setPosition(20,510);
        if(CollisionBoiteVector(m_sprite_bouton,souris)&& !select_position)
        {
            m_sprite_bouton.setTextureRect(IntRect(0,245,120,275));
            if(cliqueDroit)
            {
                if(zoom < 5)
                    zoom ++;
            }
        }
        tmp_string.setString("Zoom ++");
        tmp_string.setPosition(25,505);
        app->draw(m_sprite_bouton);
        app->draw(tmp_string);
        m_sprite_bouton.setTextureRect(IntRect(0,215,120,245));
        m_sprite_bouton.setPosition(600,510);
        if(CollisionBoiteVector(m_sprite_bouton,souris)&& !select_position)
        {
            m_sprite_bouton.setTextureRect(IntRect(0,245,120,275));
            if(cliqueDroit)
            {
                clock.restart();
                select_position = true;
                cliqueDroit = false;
            }
        }
        tmp_string.setString("Position");
        tmp_string.setPosition(605,505);
        app->draw(m_sprite_bouton);
        app->draw(tmp_string);
        if(select_position)
        {
            if((souris.x < 250 || souris.x > 550 || souris.y < 100 || souris.y > 500) && sf::Mouse::isButtonPressed(Mouse::Left) && clock.getElapsedTime().asSeconds()>1)
            {
                select_position = false;
                cout << "fin menu" << endl;
            }
            if(CollisionBoiteVector(m_sprite_collision_rect,souris) && sf::Mouse::isButtonPressed(Mouse::Left) && souris.x >250+45 && souris.x < 250+205+45)
            {
                int x = (m_sprite_collision_rect.getPosition().x-250-45)/205*(m_image_collision.getSize().x-2460);
                m_sprite_collision.setTextureRect(IntRect(x,0,x+2460,600));
                m_sprite_collision.setScale(0.083,0.083);
                m_sprite_collision_rect.setPosition(souris.x,m_sprite_collision_rect.getPosition().y);
                curseur_position = x;
            }
            app->draw(m_sprite_collision);
            app->draw(m_sprite_select);

            m_sprite_bouton.setTextureRect(IntRect(0,215,120,245));
            m_sprite_bouton.setPosition(260,250);
            if(CollisionBoiteVector(m_sprite_bouton,souris))
            {
                if(sf::Mouse::isButtonPressed(Mouse::Left))
                {
                    selectionner = 0;
                    m_sprite_collision_rect.setPosition(((posx[0])/taille*205)+295,m_sprite_collision_rect.getPosition().y);
                    m_sprite_curseur.setPosition(250+45,m_sprite_curseur.getPosition().y);
                    curseur_position = ((posx[0])/taille*205)+295;
                    int x(curseur_position);
                    m_sprite_collision.setTextureRect(IntRect(x,0,x+2460,600));
                    m_sprite_collision.setScale(0.083,0.083);
                }
            }
            if(selectionner == 0)
                m_sprite_bouton.setTextureRect(IntRect(0,245,120,275));
            tmp_string.setString("Rouge");
            tmp_string.setPosition(265,245);
            app->draw(m_sprite_bouton);
            app->draw(tmp_string);
            m_sprite_bouton.setTextureRect(IntRect(0,215,120,245));
            m_sprite_bouton.setPosition(400,250);
            app->draw(m_sprite_bouton);
            tmp_string.setString(to_string(posx[0]));
            tmp_string.setPosition(405,245);
            app->draw(tmp_string);
            m_sprite_bouton.setPosition(260,300);
            if(CollisionBoiteVector(m_sprite_bouton,souris))
            {
                if(sf::Mouse::isButtonPressed(Mouse::Left))
                {
                    selectionner = 1;
                    m_sprite_collision_rect.setPosition(((posx[1])/taille*205)+295,m_sprite_collision_rect.getPosition().y);
                    m_sprite_curseur.setPosition(250+45,m_sprite_curseur.getPosition().y);
                    curseur_position = ((posx[1])/taille*205)+295;
                    int x(curseur_position);
                    m_sprite_collision.setTextureRect(IntRect(x,0,x+2460,600));
                    m_sprite_collision.setScale(0.083,0.083);
                }
            }
            if(selectionner == 1)
                m_sprite_bouton.setTextureRect(IntRect(0,245,120,275));
            tmp_string.setString("Bleue");
            tmp_string.setPosition(265,295);
            app->draw(m_sprite_bouton);
            app->draw(tmp_string);
            m_sprite_bouton.setTextureRect(IntRect(0,215,120,245));
            m_sprite_bouton.setPosition(400,300);
            app->draw(m_sprite_bouton);
            tmp_string.setString(to_string(posx[1]));
            tmp_string.setPosition(405,295);
            app->draw(tmp_string);
            m_sprite_bouton.setPosition(260,350);
            if(CollisionBoiteVector(m_sprite_bouton,souris))
            {
                if(sf::Mouse::isButtonPressed(Mouse::Left))
                {
                    selectionner = 2;
                    m_sprite_collision_rect.setPosition(((posx[2])/taille*205)+295,m_sprite_collision_rect.getPosition().y);
                    m_sprite_curseur.setPosition(250+45,m_sprite_curseur.getPosition().y);
                    curseur_position = ((posx[2])/taille*205)+295;
                    int x(curseur_position);
                    m_sprite_collision.setTextureRect(IntRect(x,0,x+2460,600));
                    m_sprite_collision.setScale(0.083,0.083);
                }
            }
            if(selectionner == 2)
                m_sprite_bouton.setTextureRect(IntRect(0,245,120,275));
            tmp_string.setString("Vert");
            tmp_string.setPosition(265,345);
            app->draw(m_sprite_bouton);
            app->draw(tmp_string);
            m_sprite_bouton.setTextureRect(IntRect(0,215,120,245));
            m_sprite_bouton.setPosition(400,350);
            app->draw(m_sprite_bouton);
            tmp_string.setString(to_string(posx[2]));
            tmp_string.setPosition(405,345);
            app->draw(tmp_string);
            m_sprite_bouton.setPosition(260,400);
            if(CollisionBoiteVector(m_sprite_bouton,souris))
            {
                if(sf::Mouse::isButtonPressed(Mouse::Left))
                {
                    selectionner = 3;
                    m_sprite_collision_rect.setPosition(((posx[3])/taille*205)+295,m_sprite_collision_rect.getPosition().y);
                    m_sprite_curseur.setPosition(250+45,m_sprite_curseur.getPosition().y);
                    curseur_position = ((posx[3])/taille*205)+295;
                    int x(curseur_position);
                    m_sprite_collision.setTextureRect(IntRect(x,0,x+2460,600));
                    m_sprite_collision.setScale(0.083,0.083);
                }
            }
            if(selectionner == 3)
                m_sprite_bouton.setTextureRect(IntRect(0,245,120,275));
            tmp_string.setString("Jaune");
            tmp_string.setPosition(265,395);
            app->draw(m_sprite_bouton);
            app->draw(tmp_string);
            m_sprite_bouton.setTextureRect(IntRect(0,215,120,245));
            m_sprite_bouton.setPosition(400,400);
            app->draw(m_sprite_bouton);
            tmp_string.setString(to_string(posx[3]));
            tmp_string.setPosition(405,395);
            app->draw(tmp_string);
            if(CollisionBoiteVector(m_sprite_curseur, souris) && sf::Mouse::isButtonPressed(Mouse::Left))
            {
                if(souris.x > 250+45 && souris.x < 250+205+45)
                {
                    m_sprite_curseur.setPosition(souris.x,m_sprite_curseur.getPosition().y);
                    posx[selectionner] = curseur_position+(souris.x-295)/205*2460;
                }
            }
                    posx[selectionner] = curseur_position+(m_sprite_curseur.getPosition().x-295)/205*2460;
            app->draw(m_sprite_curseur);
            app->draw(m_sprite_collision_rect);
        }
        app->display();
    }
    m_image_collision.copyToImage().setPixel(posx[0], 100, Color::Red);
    m_image_collision.copyToImage().setPixel(posx[1], 100, Color::Blue);
    m_image_collision.copyToImage().setPixel(posx[2], 100, Color::Green);
    m_image_collision.copyToImage().setPixel(posx[3], 100, Color::Yellow);
    m_image_collision.copyToImage().saveToFile("terrain/temporaire/terrain.png");
}
Interface_Option::Interface_Option()
{
    Option m_option;
    if(!m_image_bouton.loadFromFile("picture/menu/bouton_generateur.png"))
    {
        cout << "Impossible de charger les boutons..." << endl;
    }
    else
    {
        m_volume.setTexture(m_image_bouton);
        m_volume.setTextureRect(IntRect(0,120,10,130));
        m_volume.setOrigin(5,5);
        m_volume.setPosition(75+(volume*2),448);
        m_qualite.setTexture(m_image_bouton);
        m_qualite.setTextureRect(IntRect(0,215,120,245));
        m_qualite.setPosition(115,220);
        m_key.setTexture(m_image_bouton);
        m_key.setTextureRect(IntRect(0,215,120,245));
    }
    if(!font.loadFromFile("font.ttf"))
    {
        cout << "Impossible de charger la police..." << endl;
    }
    else
    {
        m_texte_volume.setFont(font);
        m_texte_volume.setCharacterSize(20);
        m_texte_qualite.setFont(font);
        m_texte_qualite.setCharacterSize(20);
        string tmp_string(to_string(m_option.GetWidth()));
        tmp_string += "x";
        tmp_string += to_string(m_option.GetHeight());
        m_texte_qualite.setString(tmp_string.c_str());
        m_texte_qualite.setPosition(175-(m_texte_qualite.getLocalBounds().width/2),225);
        width = m_option.GetWidth();
        m_texte_key.setFont(font);
        m_texte_key.setCharacterSize(20);
    }
    if(!m_image_principal.loadFromFile("picture/menu/option.png"))
    {
        cout << "Impossible de charger le font ..." << endl;
    }
    else
    {
        m_fond.setTexture(m_image_principal);
    }
}
void Interface_Option::boucle(sf::RenderWindow *app)
{
    bool Is_Open(app->isOpen()), souris(false), souris_release(false);
    while(Is_Open)
    {
        souris = false;
        if(sf::Mouse::isButtonPressed(Mouse::Left) && !souris_release)
        {
            souris = true;
            souris_release = true;
        }
        if(!sf::Mouse::isButtonPressed(Mouse::Left))
        {
            souris_release = false;
        }
        Event event;
        while(app->pollEvent(event))
        {
            if(event.type == Event::Closed)
            {
                app->close();
                Is_Open = false;
            }
            if(event.type == Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
            {
                Is_Open = false;
            }
        }
        if(sf::Mouse::isButtonPressed(Mouse::Left) && sf::Mouse::getPosition().x>75 && sf::Mouse::getPosition().x < 275 && sf::Mouse::getPosition().y < 460 && sf::Mouse::getPosition().y > 435)
        {
            m_volume.setPosition(sf::Mouse::getPosition().x, m_volume.getPosition().y);
            volume = (sf::Mouse::getPosition().x-75)/2;
            m_option.ChangerOption("VOLUME", volume);
        }
        if(souris && CollisionBoiteVector(m_qualite, sf::Vector2f(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y)))
        {
            switch(width)
            {
                case 800:
                width = 1024;
                m_option.ChangerOption("LARGEUR", 1024);
                m_option.ChangerOption("HAUTEUR", 768);
                break;
                case 1024:
                width = 1280;
                m_option.ChangerOption("LARGEUR", 1280);
                m_option.ChangerOption("HAUTEUR", 1024);
                break;
                case 1280:
                width = 1600;
                m_option.ChangerOption("LARGEUR", 1600);
                m_option.ChangerOption("HAUTEUR", 1200);
                break;
                case 1600:
                width = 800;
                m_option.ChangerOption("LARGEUR", 800);
                m_option.ChangerOption("HAUTEUR", 600);
                break;
            }
            string tmp_string(to_string(m_option.GetWidth()));
            tmp_string += "x";
            tmp_string += to_string(m_option.GetHeight());
            m_texte_qualite.setString(tmp_string.c_str());
            m_texte_qualite.setPosition(175-(m_texte_qualite.getLocalBounds().width/2),225);
        }
        app->clear();
        app->draw(m_fond);
        app->draw(m_qualite);
        app->draw(m_volume);
        app->draw(m_texte_qualite);
        app->display();
    }
}
